package com.example.testdemo.rabbitmq;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

//import com.woniu.springboot_rabbitmq_producter.pojo.Msg;

@Component
public class Consumer {


    // 消费者的监听器（@RabbitListener）：交换机及其绑定的队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "msg_queue", declare = "true"),
            exchange = @Exchange(name = "springboot-exchange", declare = "true", type = "topic"),
            key = "#"
    ))
    // 处理类
    @RabbitHandler
    public void recv(@Payload String msg, Channel channel, @Headers Map headers) {
        System.out.println("消费者：" + msg);
        Long tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(tag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
